package com.ruoyi.manage.enterprise.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.enterprise.domain.EcEnterpriseLegal;
import com.ruoyi.manage.enterprise.domain.vo.EcEnterpriseLegalVo;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseLegalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 企业法人Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@Api(tags="企业法人控制器")
@RestController
@RequestMapping("/manage/legal" )
public class EcEnterpriseLegalController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IEcEnterpriseLegalService ecEnterpriseLegalService;

    /**
     * 查询企业法人列表
     */
    @ApiOperation(value = "查询企业法人",notes = "企业法人管理")
    @PreAuthorize("@ss.hasPermi('manage:legal:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(EcEnterpriseLegalVo ecEnterpriseLegal,QueryWrapper<EcEnterpriseLegal> queryWrapper) {
        setQueryCondition(ecEnterpriseLegal,queryWrapper);
        startPage();
        List<EcEnterpriseLegal> list = ecEnterpriseLegalService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(EcEnterpriseLegalVo ecEnterpriseLegal, QueryWrapper<EcEnterpriseLegal> queryWrapper) {
        if(StrUtil.isNotBlank(ecEnterpriseLegal.getName())){
            queryWrapper.like(TABLE_PREFIX+"name" ,ecEnterpriseLegal.getName());
        }
        if(StrUtil.isNotBlank(ecEnterpriseLegal.getMobile())){
            queryWrapper.eq(TABLE_PREFIX+"mobile" ,ecEnterpriseLegal.getMobile());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出企业法人列表
     */
    @PreAuthorize("@ss.hasPermi('manage:legal:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, EcEnterpriseLegalVo ecEnterpriseLegal,QueryWrapper<EcEnterpriseLegal> queryWrapper) {
        if (CollUtil.isEmpty( ecEnterpriseLegal.getIds())){
            setQueryCondition(ecEnterpriseLegal,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",ecEnterpriseLegal.getIds());
        }
        List<EcEnterpriseLegal> list = ecEnterpriseLegalService.list(queryWrapper);
        ExcelUtil<EcEnterpriseLegal> util = new ExcelUtil<EcEnterpriseLegal>(EcEnterpriseLegal. class);
        util.exportExcel(response,list, "legal");
    }

    /**
     * 获取企业法人详细信息
     */
    @ApiOperation(value = "获取企业法人",notes = "企业法人管理")
    @PreAuthorize("@ss.hasPermi('manage:legal:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseLegalService.queryGetById(id));
    }


    /**
     * 获取企业法人详细信息
     */
    @ApiOperation(value = "获取企业法人",notes = "企业法人管理")
    @GetMapping(value = "/single/{id}" )
    public AjaxResult getInfoSingle(@PathVariable("id" ) Long id) {
        return AjaxResult.success(ecEnterpriseLegalService.getOne(new QueryWrapper<EcEnterpriseLegal>().eq("enterprise_id",id),false));
    }

    /**
     * 新增企业法人
     */
    @ApiOperation(value = "新增企业法人",notes = "企业法人管理")
    @PreAuthorize("@ss.hasPermi('manage:legal:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody EcEnterpriseLegal ecEnterpriseLegal) {
        return toAjax(ecEnterpriseLegalService.addEcEnterpriseLegal(ecEnterpriseLegal) ? 1 : 0);
    }




    /**
     * 修改企业法人
     */
    @ApiOperation(value = "修改企业法人",notes = "企业法人管理")
    @PreAuthorize("@ss.hasPermi('manage:legal:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody EcEnterpriseLegal ecEnterpriseLegal) {
        return toAjax(ecEnterpriseLegalService.updateEcEnterpriseLegal(ecEnterpriseLegal) ? 1 : 0);
    }

    /**
     * 删除企业法人
     */
    @ApiOperation(value = "删除企业法人",notes = "企业法人管理")
    @PreAuthorize("@ss.hasPermi('manage:legal:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ecEnterpriseLegalService.deleteEcEnterpriseLegalByIds(ids) ? 1 : 0);
    }
}
